# frozen_string_literal: true

require 'fast_spec_helper'
require 'cvss_suite'

RSpec.describe Gitlab::VulnerabilityScanning::Advisory, feature_category: :software_composition_analysis do
  let(:cvss_v3) { nil }
  let(:cvss_v2) { nil }
  let(:affected_component) { build(:vs_affected_component) }

  subject(:advisory) do
    described_class.new(
      xid: '051e7fdd-4e0a-4dfd-ba52-083ee235a580',
      title: 'The title goes here',
      description: 'The description goes here',
      solution: 'The solution goes here',
      identifiers: ['CVE-2023-00001'],
      urls: ["https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2023-00001"],
      cvss_v3: cvss_v3,
      cvss_v2: cvss_v2,
      affected_component: affected_component)
  end

  describe '#cvss_v3' do
    context 'when cvss_v3 is nil' do
      it { expect(advisory.cvss_v3).to be_nil }
    end

    context 'when cvss_v3 is a string' do
      let(:cvss_v3) { 'CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H' }

      it { expect(advisory.cvss_v3).to be_instance_of(CvssSuite::Cvss3) }
      it { expect(advisory.cvss_v3.vector).to eq('CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H') }
    end

    context 'when cvss_v3 is a CvssSuite::Cvss31 object' do
      let(:cvss_v3) { CvssSuite::Cvss31.new('CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H') }

      it { expect(advisory.cvss_v3).to be(cvss_v3) }
    end

    context 'when cvss_v3 is a CvssSuite::Cvss3 object' do
      let(:cvss_v3) { CvssSuite::Cvss3.new('CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H') }

      it { expect(advisory.cvss_v3).to be(cvss_v3) }
    end
  end

  describe '#cvss_v2' do
    context 'when cvss_v2 is nil' do
      it { expect(advisory.cvss_v2).to be_nil }
    end

    context 'when cvss_v2 is a string' do
      let(:cvss_v2) { "AV:N/AC:L/Au:N/C:N/I:N/A:P" }

      it { expect(advisory.cvss_v2).to be_instance_of(CvssSuite::Cvss2) }
      it { expect(advisory.cvss_v2.vector).to eq('AV:N/AC:L/Au:N/C:N/I:N/A:P') }
    end

    context 'when cvss_v2 is a CvssSuite::Cvss2 object' do
      let(:cvss_v2) { CvssSuite::Cvss2.new("AV:N/AC:L/Au:N/C:N/I:N/A:P") }

      it { expect(advisory.cvss_v2).to be(cvss_v2) }
    end
  end
end
